﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;

namespace PBO.Data.Util
{
    public static class CalculateHelper
    {

        #region 催醒

        //http://wiki.52poke.com/wiki/催醒计算

        public static BattleType GetHiddenType(this IStats iv)
        {
            var array = (from type in BaseStats.Types select iv[type]).ToArray();
            int id = GetHiddenTypeId(array);
            return (BattleType)(id + 2);
        }

        public static int GetHiddenPower(this IStats iv)
        {
            var array = (from type in BaseStats.Types select iv[type]).ToArray();
            return GetHiddenPower(array);
        }

        public static int GetHiddenTypeId(int[] array)
        {
            int sum = 0;
            for (int i = 0; i < array.Length; i++)
            {
                sum += (array[i] & 1) << i;
            }
            return sum * 15 / 63;
        }

        public static byte GetHiddenPower(int[] array)
        {
            int sum = 0;
            for (int i = 0; i < array.Length; i++)
            {
                sum += array[i] % 4 > 1 ? 1 << i : 0;
            }
            return Convert.ToByte(sum * 40 / 63 + 30);
        }

        #endregion

        #region base 6d

        public static BaseStats GetBaseValue(PokemonNature nature, BaseStats bv, BaseStats iv, BaseStats ev, int lv)
        {
            var bs = new BaseStats();
            foreach (StatType stat in BaseStats.Types)
            {
                if (stat == StatType.Hp)
                {
                    bs[stat] = GetHp(bv[stat], ev[stat], iv[stat], lv);
                }
                else
                {
                    bs[stat] = ComputeAbility(bv[stat], ev[stat], iv[stat], nature.StatRevise(stat), lv);
                }
            }
            return bs;
        }

        #endregion

        public static int GetHp(int typeBase, int ev, int iv, int lv)
        {
            return typeBase == 1 ? 1 : (((typeBase << 1) + iv + (ev >> 2)) * lv / 100 + 10 + lv);
        }

        public static int ComputeAbility(int bv, int ev, int iv, int nv, int lv)
        {
            return Convert.ToInt32(((bv * 2 + iv + ev / 4) * lv / 100 + 5) * nv / 10);
        }
    }
}
